/**
 * Copyright (c) 2016,http://www.365wuliu.com/  All Rights Reserved.
 */
package com.cargo.base.page;

import com.github.pagehelper.Page;
import com.wordnik.swagger.annotations.ApiModelProperty;
import org.apache.ibatis.session.RowBounds;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * Page:分页的主类
 * 
 * @Project Name:cargo
 * @File Name:Page.java
 * @Package Name:com.cargo.common.page
 * @Creator:wizard
 * @Date:2016年4月14日下午1:50:51
 */
public class PageResult extends RowBounds implements Serializable {

	private static final long serialVersionUID = -1993051712091479583L;

	private static final int DEFAULT_PAGESIZE = 10;

	public static final PageResult DEFAULT = new PageResult();

	@ApiModelProperty(value = "页码，从1开始")
	private Integer pageNo = 1; // 页码(当前页)
	@ApiModelProperty(value = "每页多少条，不填则默认10条")
	private Integer pageSize = DEFAULT_PAGESIZE; // 每页的记录数量
	@ApiModelProperty(value = "总页数")
	private Integer totalPage = 0; // 总页数
	@ApiModelProperty(value = "总记录数")
	private Integer totalCount = 0; // 总记录数
	@ApiModelProperty(value = "具体的数据集合")
	private List<?> result = Collections.emptyList();// 数据列
	@ApiModelProperty(value = "是否有上一页")
	private boolean hasPre = false;
	@ApiModelProperty(value = "是否有下一页")
	private boolean hasNext = false;
	@ApiModelProperty(value = "上一页页码")
	private Integer prePage = 0;
	@ApiModelProperty(value = "下一页页码")
	private Integer nextPage = 0;
	@ApiModelProperty(value = "系统自动处理时的变量，不要对其有任何操作")
	private Integer offset = 0;//表示从表中第几行开始取数据
	@ApiModelProperty(value = "系统自动处理时的变量，不要对其有任何操作")
	private Integer limit = 0; //表示 返回多少条数据
	@ApiModelProperty(value = "排序的列名")
	private String orderColumn;

	public PageResult() {
	}

	public PageResult(int pageSize) {
		setPageSize(pageSize);
	}

	public PageResult(int pageNo, int pageSize) {
		setPageNo(pageNo);
		setPageSize(pageSize);
	}

	public PageResult(int pageNo, int pageSize, int totalCount, List<?> result) {
		setPageNo(pageNo);
		setPageSize(pageSize);
		setTotalCount(totalCount);
		setResult(result);
	}
	/**
	 * wizard 适应本项目的分页=======
	 */
	public PageResult(List<?> result) {
		setPageNo(((Page<?>) result).getPageNum());
		setPageSize(((Page<?>) result).getPageSize());
		setTotalCount((int) ((Page<?>) result).getTotal());
		setOrderColumn(((Page<?>)result).getOrderBy());
		setResult(result);
	}


	/**
	 * 获得当前页的页号,序号从1开始,默认为1.
	 */
	public int getPageNo() {
		return pageNo;
	}

	/**
	 * 设置当前页的页号,序号从1开始,低于1时自动调整为1.
	 */
	public void setPageNo(Integer pageNo) {
		if(pageNo==null){
			this.pageNo = 1;
		}else{
			this.pageNo = (pageNo < 1) ? 1 : pageNo;
		}
	}

	/**
	 * 获得每页的记录数量,默认为DEFAULT_PAGESIZE.
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 设置每页的记录数量,低于1时自动调整为DEFAULT_PAGESIZE.
	 */
	public void setPageSize(Integer pageSize) {
		if(pageSize==null){
			this.pageSize = DEFAULT_PAGESIZE;
		}else{
			this.pageSize = (pageSize < 1) ? DEFAULT_PAGESIZE : pageSize;
		}
	}

	/**
	 * 取得总记录数, 默认值为0.
	 */
	public int getTotalCount() {
		return totalCount;
	}

	/**
	 * 设置总记录数, 默认值为0.
	 */
	public void setTotalCount(int totalCount) {
		this.totalCount = (totalCount < 0) ? 0 : totalCount;
	}

	/**
	 * 总页数.
	 */
	public int getTotalPage() {
		totalPage = totalCount / pageSize;
		if(totalPage==0){
			totalPage=1;
		}else if (totalCount % pageSize > 0) {
			totalPage++;
		}
	 
		return totalPage;
	}

	/**
	 * 页内的数据列表.
	 */
	public List<?> getResult() {
		return result;
	}

	public void setResult(List<?> result) {
		this.result = result;
	}

	/**
	 * 是否还有上一页.
	 */
	public boolean getHasPre() {
		return pageNo - 1 >= 1;
	}

	/**
	 * 是否还有下一页.
	 */
	public boolean getHasNext() {
		return pageNo + 1 <= getTotalPage();
	}

	/**
	 * 取得上页的页号,序号从1开始.
	 */
	public int getPrePage() {
		if (getHasPre())
			return pageNo - 1;
		else
			return pageNo;
	}

	/**
	 * 取得下页的页号,序号从1开始.
	 */
	public int getNextPage() {
		if (getHasNext())
			return pageNo + 1;
		else
			return pageNo;
	}

	/**
	 * =========================================================================
	 * ============
	 */

	/**
	 * 偏移量,用于sql分页查询(从0开始)
	 */
	public int getOffset() {
		return ((pageNo - 1) * pageSize);
	}

	public int getLimit() {
		return pageSize;
	}

	@Override
	public String toString() {
		return getClass().getName() + " [pageNo=" + getPageNo() + ",pageSize=" + getPageSize() + ",totalPage="
				+ getTotalPage() + ",totalCount=" + getTotalCount() + ",resultSize=" + getResult().size() + "]";
	}

	public String getOrderColumn() {
		return orderColumn;
	}

	public void setOrderColumn(String orderColumn) {
		if(orderColumn!=null && (!orderColumn.equals(""))){
			this.orderColumn = orderColumn;
		}
	}
}
